package com.example.routeplansystem.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.read.listener.PageReadListener;
import com.example.routeplansystem.constant.ResponseStatus;
import com.example.routeplansystem.constant.SessionMessage;
import com.example.routeplansystem.dao.VehicleDao;
import com.example.routeplansystem.entity.bo.VehicleExcelBO;
import com.example.routeplansystem.entity.dto.ResponseEntity;
import com.example.routeplansystem.entity.po.NodePojo;
import com.example.routeplansystem.entity.po.VehicleMessage;
import com.example.routeplansystem.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 唐海军
 * @create 2022-05-11 7:52
 */

@Component
@Configuration
public class VehicleBatchImportService {

    @Autowired
    private VehicleDao vehicleDao;

    public ResponseEntity batchImport(MultipartFile mFile, HttpServletRequest request, int questionId) {
        ExcelUtil.checkFile(mFile);
        InputStream inputStream = null;
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setMsg("成功！！！");
        List<VehicleExcelBO> list = new ArrayList<>();
        try {
            inputStream = mFile.getInputStream();
            EasyExcel.read(inputStream, VehicleExcelBO.class, new PageReadListener<VehicleExcelBO>(dataList -> {
                list.addAll(dataList);
            })).sheet().doRead();
        } catch (ExcelCommonException e) {
            throw new RuntimeException("只允许上传Excel文件");
        } catch (IOException e) {
            throw new RuntimeException("文件IO错误");
        } finally {
            try {
                Objects.requireNonNull(inputStream, "获取文件输入流失败");
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        List<VehicleMessage> vehicleMessages = list.stream()
                .map(vehicleMessage -> vehicleMessage.toPojo(questionId)).collect(Collectors.toList());

        for (int i = 0; i < vehicleMessages.size(); i++) {
            VehicleMessage vehicleMessage = vehicleMessages.get(i);
            if (vehicleMessage.getVehicleNumber() == null) continue; // EasyExcel会产生空行
            if (vehicleMessage.getDate().after(new Date())) {
                responseEntity.setStatus(1);
                responseEntity.setMsg("车辆日期出错，不能比当前时间还晚，下次从" + i + 1 + "行,开始导入");
                return responseEntity;
            }

            boolean insertFlag = vehicleDao.insertVehicle(vehicleMessage);
            if (!insertFlag) {
                responseEntity.setStatus(ResponseStatus.SERVER_EXCUTE_FAIL);
                responseEntity.setMsg("服务器错误, 请下次从第" + i + 1 + "行数据开始导入");
                return responseEntity;
            }
        }
        return responseEntity;
    }
}
